package gdut.imis.controller;

import gdut.imis.Utils.Result;
import gdut.imis.domain.University;
import gdut.imis.service.UniversityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description
 * @Author JiaFu
 * @Date 2023/4/7 8:56
 */
@RestController
@RequestMapping("/university")
@Api(value = "大学接口", tags = {"大学接口"})
@RequiredArgsConstructor
public class UniversityController extends BaseController {

    private final UniversityService universityService;

    @ApiOperation("获取所有大学列表")
    @GetMapping("getAllUniversity")
    public Result getAllUniversity() {
        List<University> list = universityService.list();
        return Result.success(list);
    }

    @ApiOperation("新增一个大学")
    @PostMapping("/addUniversity")
    @PreAuthorize("@cp.hasPermission('ONE_LEVAL_MANAGER')")
    public Result addUniversity(@RequestBody University university) {
        return universityService.addUniversity(university);
    }

    @PostMapping("/update")
    @ApiOperation("更新大学")
    public Object update(@RequestBody University university) {
        return universityService.updateById(university);
    }

    @ApiOperation("根据id获取大学")
    @GetMapping("/getUniversityById/{id}")
    public Result getUniversityById(@PathVariable("id") Long id) {
        return universityService.getUniversityById(id);
    }

    @ApiOperation("分页查询所有高校（只有一级管理员才有的权限）")
    @GetMapping("/getPageUniversity/{pageNo}/{pageSize}")
    @PreAuthorize("@cp.hasPermission('ONE_LEVAL_MANAGER')")
    public Result getPageUniversity(@PathVariable("pageNo") Integer pageNo, @PathVariable("pageSize") Integer pageSize) {
        Result result = this.checkPageParams(pageNo, pageSize);
        if (result.getCode() == 0) {
            return universityService.getPageUniversity(pageNo, pageSize);
        }
        return result;
    }

    @ApiOperation("根据id删除大学")
    @DeleteMapping("/deleteUniversityById/{id}")
    @PreAuthorize("@cp.hasPermission('ONE_LEVAL_MANAGER')")
    public Result deleteUniversityById(@PathVariable("id")Long id) {
        return universityService.deleteUniversityById(id);
    }

}
